<template>
  <el-form class="" :model="file" label-width="80px">
    <!-- <el-form-item label="文件名称"><el-input v-model="file.fileName"></el-input></el-form-item> -->
    <el-form-item>
      <el-upload
        class="upload-demo"
        ref="uploadRef"
        :name="file.fileName"
        action="/api/files/upload"
        :on-change="handleChange"
        :file-list="fileList"
        :auto-upload="false"
      >
        <!-- <i class="el-icon-upload"></i> -->
        <el-button type="primary" size="small">选取文件</el-button>
        <template #tip>
          <div class="el-upload__tip">选择要上传的文件，且不超过2M</div>
        </template>
      </el-upload>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="add">添加</el-button>
      <el-button @click="goBack">返回</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { ref } from "vue";
import { useRouter, useRoute } from "vue-router";
import { ElMessage } from "element-plus";

const OK = 200;
const router = useRouter();
const route = useRoute();
const uploadRef = ref(null);

const file = ref({});
const fileList = ref([]);
const fileTypeId = ref(3);

const add = () => {
  uploadRef.value.submit();
};

const goBack = () => {
  router.back();
};

const handleChange = (file, fileList) => {
  const response = file.response;
  if (response && response.code === OK) {
    ElMessage.success("上传文件成功");
    router.push({ name: "FileList", query: { fileTypeId: fileTypeId.value } });
  }
  fileList.value = fileList.slice(-3);
};
</script>

<style scoped="scoped">
</style>
